1
Introducción a PyTorch: ¿Por qué los tensores son importantes?
EvoClass-AI002Lecture 1
00:00

Introducción a PyTorch: ¿Por qué los tensores son importantes?

PyTorch es un marco de código abierto altamente flexible y dinámico, muy valorado para la investigación en aprendizaje profundo y el desarrollo rápido de prototipos. En su núcleo, el tensor es la estructura de datos indispensable. Es un arreglo multidimensional diseñado para manejar eficientemente las operaciones numéricas necesarias para modelos de aprendizaje profundo, con soporte automático para aceleración en aceleración en GPU automatically.

1. Comprender la estructura del tensor

Cada entrada, salida y parámetro del modelo en PyTorch está encapsulado en un tensor. Sirven para el mismo propósito que los arreglos de NumPy, pero están optimizados para procesamiento en hardware especializado como GPUs, lo que los hace mucho más eficientes para las operaciones de álgebra lineal a gran escala requeridas por las redes neuronales.

Las propiedades clave definen el tensor:

  • Forma: Define las dimensiones de los datos, expresadas como una tupla (por ejemplo, $4 \times 32 \times 32$ para un lote de imágenes).
  • Tipo de dato: Indica el tipo numérico de los elementos almacenados (por ejemplo, torch.float32 para los pesos del modelo, torch.int64 para indexación).
  • Dispositivo: Indica la ubicación física del hardware: típicamente 'cpu' o 'cuda' (GPU de NVIDIA).
Grafo dinámico y Autograd
PyTorch utiliza un modelo de ejecución imperativo, lo que significa que el grafo computacional se construye mientras se ejecutan las operaciones. Esto permite al motor integrado de diferenciación automática, Autograd, rastrear cada operación sobre un tensor, siempre que se establezca la propiedad requires_grad=True esté activada, permitiendo el cálculo sencillo de gradientes durante la propagación hacia atrás.
fundamentals.py
TERMINALbash — pytorch-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR Live

Run code to inspect active tensors
Question 1
Which command creates a $5 \times 5$ tensor containing random numbers following a uniform distribution between 0 and 1?
torch.rand(5, 5)
torch.random(5, 5)
torch.uniform(5, 5)
torch.randn(5, 5)
Question 2
If tensor $A$ is on the CPU, and tensor $B$ is on the CUDA device, what happens if you try to compute $A + B$?
An error occurs because operations require tensors on the same device.
PyTorch automatically moves $A$ to the CUDA device and proceeds.
The operation is performed on the CPU, and the result is returned to the CPU.
Question 3
What is the most common data type (dtype) used for model weights and intermediate calculations in Deep Learning?
torch.float32 (single-precision floating point)
torch.int64 (long integer)
torch.bool
torch.float64 (double-precision floating point)
Challenge: Tensor Manipulation and Shape
Prepare a tensor for a specific matrix operation.
You have a feature vector $F$ of shape $(10,)$. You need to multiply it by a weight matrix $W$ of shape $(10, 5)$. For matrix multiplication (MatMul) to work, $F$ must be 2-dimensional.
Step 1
What should the shape of $F$ be before multiplication with $W$?
Solution:
The inner dimensions must match, so $F$ must be $(1, 10)$. Then $(1, 10) @ (10, 5) \rightarrow (1, 5)$.
Code: F_new = F.unsqueeze(0) or F_new = F.view(1, -1)
Step 2
Perform the matrix multiplication between $F_{new}$ and $W$ (shape $(10, 5)$).
Solution:
The operation is straightforward MatMul.
Code: output = F_new @ W or output = torch.matmul(F_new, W)
Step 3
Which method explicitly returns a tensor with the specified dimensions, allowing you to flatten the tensor back to $(50,)$? (Assume $F$ was $(5, 10)$ initially and is now flattened.)
Solution:
Use the view or reshape methods. The fastest way to flatten is often using -1 for one dimension.
Code: F_flat = F.view(-1) or F_flat = F.reshape(50)